﻿using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void SelectBottom()
        {
            if (Globals.selectedEditor == null) return;

            //正常模式，选取公式。
            if (Globals.selectedEditor.SelectedExpression == null)
            {
                if (Globals.selectedEditor.RootPanel != null)
                {
                    Globals.selectedEditor.SelectedExpression
                        = Globals.selectedEditor.RootPanel;
                }

                return;
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                Expressions.SubPanel subPanel = curEx as Expressions.SubPanel;
                Expressions.SubPanel bottomPanel = subPanel.BottomSubPanel;
                if (bottomPanel != null)
                {
                    curEx.Editor.SelectedExpression = bottomPanel;
                }
                else
                {
                    if (subPanel.ParentExpression != null)
                    {
                        curEx.Editor.SelectedExpression = subPanel.ParentExpression;
                    }
                }
                return;
            }

            if (curEx.Parent is StackPanel)
            {
                StackPanel spanel = curEx.Parent as StackPanel;

                if (spanel.Orientation == Orientation.Vertical)
                {
                    int curIndex = spanel.Children.IndexOf(curEx);
                    if (curIndex < spanel.Children.Count - 1)
                    {
                        if (spanel.Children[curIndex + 1] is Expressions.Expression)
                        {
                            if ((spanel.Children[curIndex + 1] as Expressions.Expression).Visibility != Visibility.Visible)
                            {
                                //如果不可见，说明可能是被当前公式给“折叠”了，
                                //而当前公式应是个“标题文本块”。
                                //这里用简便的实现方法，直接取“下一个可见公式”。
                                int tmpIndex = curIndex + 1;
                                Expressions.Expression nextVisibleEx = null;
                                while (tmpIndex < spanel.Children.Count - 1)
                                {
                                    tmpIndex += 1;
                                    Expressions.Expression tmpEx = spanel.Children[tmpIndex] as Expressions.Expression;
                                    if (tmpEx.Visibility == Visibility.Visible)
                                    {
                                        nextVisibleEx = tmpEx;
                                        break;
                                    }
                                }

                                if (nextVisibleEx != null)
                                {
                                    curEx.Editor.SelectedExpression = nextVisibleEx;
                                }
                                else
                                {
                                    curEx.Editor.SelectedExpression = curEx.ParentPanel;
                                }
                            }
                            else
                            {
                                //正常的可见公式。
                                curEx.Editor.SelectedExpression = spanel.Children[curIndex + 1] as Expressions.Expression;
                            }
                        }
                    }
                    else
                    {
                        if (spanel.Parent is Border)
                        {
                            Border mainBorder = spanel.Parent as Border;
                            if (mainBorder.Parent is Expressions.SubPanel)
                            {
                                curEx.Editor.SelectedExpression = mainBorder.Parent as Expressions.SubPanel;
                                if ((curEx.Editor.SelectedExpression as Expressions.SubPanel).SkipSelect)
                                {
                                    SelectBottom();
                                }
                            }
                        }
                    }
                }
                else
                {
                    //水平子面板中
                    if (spanel.Parent is Border)
                    {
                        Border mainBorder = spanel.Parent as Border;
                        if (mainBorder.Parent is Expressions.SubPanel)
                        {
                            curEx.Editor.SelectedExpression = mainBorder.Parent as Expressions.SubPanel;
                            if ((curEx.Editor.SelectedExpression as Expressions.SubPanel).SkipSelect)
                            {
                                SelectBottom();
                            }
                        }
                    }
                }
            }
        }
    }
}
